package com.woniu.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 用户认证鉴权的拦截器
public class LoginInterceptor implements HandlerInterceptor {

    // 拦截请求，到Controller之前执行.../home
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("去哪里：" + request.getRequestURI());
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            // 重定向到登录页
            response.sendRedirect("/login.html"); // 有点不好，绕过了Controller.
            return false;
        }
        // true放行，false不放行
        return true;
    }

    // 拦截请求，Controller之后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("之后执行");
    }
}
